var server = require('../../utils/server');
var common = require('../../utils/common');
var amapFile = require('../../libs/amap-wx.js');
const app = getApp();
Page({
  data: {
    addressList: '',
    curAddrId: "",
    fromType: "", //1:到家选择地址，0：会员专供
    fromPage: "" //hyzg:通过会员专供结算页进入选择地址;dj:到家业务选择地址
  },
  onLoad(options = {}) {
    this.setData({
      fromType: options.fromtype
    })
    this.data.fromPage = options.frompage;
  },
  onShow: function () {
    this.getAddressList();
  },
  getAddressList() {
    if (app.globalData.locateCity != "") {
      this.setData({
        isLocate: true
      })
      app.globalData.isLocate = true;
      server.postJSON(common.storePath + 'address/listforcity/', {
        "city": app.globalData.locateCity,
        "type": this.data.fromType
      }, res => {
        if (res.data.errno == '0') {
          this.setData({
            addressList: res.data.data
          })
        } else {
          common.showErrorToast(res.errmsg)
        }
      })
    } else {
      this.locateCur()
    }

  },
  // 获取定位
  locateCur() {
    var that = this;
    common.showLoading();
    var gdMap = new amapFile.AMapWX({
      key: common.gdKey
    });
    gdMap.getRegeo({
      success: resp => {
        common.hideLoading();
        app.globalData.locateLongitude = resp[0].longitude;
        app.globalData.locateLatitude = resp[0].latitude;
        app.globalData.locateCity = resp[0].regeocodeData.addressComponent.city;
        app.globalData.locateProvince = resp[0].regeocodeData.addressComponent.province;
        server.postJSON(common.storePath + 'address/listforcity/', {
          "city": app.globalData.locateCity,
          "type": this.data.fromType
        }, res => {
          if (res.data.errno == '0') {
            this.setData({
              addressList: res.data.data
            })
          } else {
            common.showErrorToast(res.errmsg)
          }
        })
      },
      fail: info => {
        that.setData({
          isLocate: false
        })
        common.hideLoading();
        app.globalData.isLocate = false
      }
    })
  },
  // 打开定位
  openLocation(e) {
    if (e.detail.errMsg == "openSetting:ok" && e.detail.authSetting['scope.userLocation']) {
      app.globalData.isLocate = true;
      this.setData({
        isLocate: true
      })
    } else {
      common.showErrorToast('需要授权地理位置后使用！');
    }
  },
  goToEditAddr: function (e) {
    var addrInfo = e.currentTarget.dataset.addrinfo;
    if (addrInfo != undefined) {
      wx.setStorage({
        key: 'curAddrInfo',
        data: addrInfo,
        success: res => {
          wx.navigateTo({
            url: '../../address_manage/pages/editAddr?addrId=' + e.currentTarget.dataset.id + '&&addrType=0'
          })
        }
      })
    } else {
      wx.navigateTo({
        url: '../../address_manage/pages/editAddr?addrId=' + e.currentTarget.dataset.id + '&&addrType=0'
      })
    }
  },
  chooseNewAddr(e) {
    var curtarget = e.currentTarget.dataset;
    if (this.data.fromPage == 'cart') {
      app.globalData.addrId = curtarget.id;
      app.globalData.longitude = curtarget.lon;
      app.globalData.latitude = curtarget.la;
      app.globalData.city = curtarget.city;
      app.globalData.location = curtarget.address;
      app.globalData.skipType = 2;
      app.globalData.refreshTag = true;
      app.globalData.isGetCartByAdd = true;
      wx.navigateBack();
      app.globalData.companyid = "";
      app.globalData.storeType = "";
      wx.setStorage({
        key: 'companyid',
        data: app.globalData.companyid
      })
    } else if (this.data.fromPage == 'hyzg') {
      app.globalData.hyzgAddrId = curtarget.id;
      wx.navigateBack();
    } else if (this.data.fromPage == 'dj') {
      app.globalData.addrId = curtarget.id;
      app.globalData.longitude = curtarget.lon;
      app.globalData.latitude = curtarget.la;
      app.globalData.city = curtarget.city;
      app.globalData.location = curtarget.address;
      app.globalData.refreshTag = true;
      wx.removeStorage({
        key: 'storeId',
        success: function (res) {
          wx.navigateBack();
        },
      })
    }
  },
  chooseOtherCityAddr(e) {
    var curtarget = e.currentTarget.dataset;
    if (this.data.fromType != 1) {
      app.globalData.hyzgAddrId = curtarget.id;
      wx.navigateBack();
    }
  }
})